/*
   @Copyright:LintCode
   @Author:   tjyemail
   @Problem:  http://www.lintcode.com/problem/reverse-linked-list
   @Language: C++
   @Datetime: 16-02-09 05:16
   */

/**
 * Definition of ListNode
 * 
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 * 
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
	/**
	 * @param head: The first node of linked list.
	 * @return: The new head of reversed linked list.
	 */
	ListNode *reverse(ListNode *head) {
		// write your code here
		ListNode *p, *t;
		for(p=head, head=NULL; p; p=t){
			t = p->next;
			p->next = head;
			head = p;
		}
		return head;
	}
};
